package com.CityHunter.OverLay;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;

import com.CityHunter.RoutePoint;
import com.CityHunter.Track;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class RouteOverLay  extends Overlay{
	private List<RoutePoint> rpList = new ArrayList<RoutePoint>();
	public RouteOverLay(List<RoutePoint> rpList){
		this.rpList=rpList;
	}
	 public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			
			Paint p = new Paint();
			Path myPath = new Path();
		     
		
			for(int i=0;i<rpList.size();i++) {
				 Point out = new Point();
				mapView.getProjection().toPixels(rpList.get(i).getPoint(), out);
				
				
				  if( i == 0 ){
                      myPath.moveTo(out.x, out.y);
                      printDesc( out, canvas, p, i);
			}
                  else if( i+1 != rpList.size() ){
                      myPath.lineTo(out.x, out.y);
		           printDesc( out, canvas, p, i);}
                  else{
                      myPath.setLastPoint(out.x, out.y);
				  printDesc( out, canvas, p, i);
                  }
			
				
			}
		

			p.setColor(Color.RED);
			   p.setStyle(Style.STROKE);
			   p.setStrokeWidth(10);
			   p.setAlpha(70);
		
			   canvas.drawPath(myPath, p); 
			   super.draw(canvas, mapView, shadow);
		 }
	 
	 public void printDesc(Point out,Canvas canvas,Paint p,int i){
	       int[] scoords = new int[2];
     		int sz = 5;
     		
     		scoords[0] = out.x;
   		scoords[1] = out.y;
             int sw = (int) (p.measureText(rpList.get(i).getDescription()) + 0.5f);
     		int sh = 25;
     		int sx = scoords[0] - sw / 2 - 5;
     		int sy = scoords[1] - sh - sz - 2;
     		RectF rec = new RectF(sx, sy, sx + sw + 10, sy + sh);

     		p.setStyle(Style.FILL);
     		p.setColor(Color.BLACK);

     		canvas.drawRoundRect(rec, 5, 5, p);

     		p.setStyle(Style.STROKE);
     		p.setARGB(255, 255, 255, 255);
     		canvas.drawRoundRect(rec, 5, 5, p);
     		// canvas.d

     		canvas.drawText(rpList.get(i).getDescription(), sx + 5, sy + sh - 8, p);

     		// Draw point body and outer ring
     		p.setStyle(Style.FILL);
     		p.setColor(Color.BLUE);
     		p.setStrokeWidth(1);
     		RectF spot = new RectF(scoords[0] - sz, scoords[1] + sz, scoords[0]
     				+ sz, scoords[1] - sz);
     		canvas.drawOval(spot, p);

     		p.setARGB(255, 255, 0, 0);
     		p.setStyle(Style.STROKE);
     		canvas.drawCircle(scoords[0], scoords[1], sz, p);
	 }
}
